/* test_ifs.c */

#include <stdlib.h>
#include <stdio.h>

#include <sys/ioctl.h>
#include <net/if.h>
#include <unistd.h>
#include <netinet/in.h>

int main(void)
{
  struct ifreq reqs[10];
  struct ifconf conf;
  int sock, i;

  conf.ifc_len = sizeof(reqs);
  conf.ifc_req = reqs;

  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock < 0) {
    printf("socket() returns error\n");
    return 1;
  }

  if (ioctl(sock, SIOCGIFCONF, &conf) < 0) {
    printf("ioctl() returns error\n");
    close(sock);
    return 1;
  }
  
  for (i = 0; i < conf.ifc_len/sizeof(struct ifreq); i++) {
    struct sockaddr_in *addr;
    
    if (reqs[i].ifr_addr.sa_family != AF_INET)
      continue;
    addr = (struct sockaddr_in *) &reqs[i].ifr_addr;
    printf("name = '%s'\n", reqs[i].ifr_name);
    printf("ip = %08x\n", addr->sin_addr.s_addr);
  }

  close(sock);
  return 0;
}
